<!--
 * @Description: 绑定第三方店铺弹窗
 * @Author: liyujie
 * @Date: 2021/10/12
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/10/12
-->
<template>
    <section class="wrapper">
        <el-dialog
            title="绑定第三方店铺"
            :visible.sync="dialogVisible"
            width="780px"
            :close-on-click-modal="false"
            @close="$emit('close')"
        >
            <div class="dialog-content">
                <el-form
                    :model="ruleForm"
                    label-width="126px"
                    @submit.native.prevent
                >
                    <el-form-item label="店铺平台">
                        <el-radio-group v-model="ruleForm.shop_social_type">
                            <el-radio :label="1">淘宝</el-radio>
                            <el-radio :label="2">京东</el-radio>
                            <el-radio :label="3">拼多多</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="通过商品查询店铺">
                        <el-input
                            v-model="content"
                            size="medium"
                            style="width: 380px"
                            clearable
                        />
                        <el-button
                            type="primary"
                            size="small"
                            class="query-button"
                            @click="searchShop"
                        >
                            查询店铺信息
                        </el-button>
                    </el-form-item>
                    <el-form-item :style="{ visibility: ruleForm.social_shop_no ? '' : 'hidden' }">
                        <div class="result-view">
                            <div class="top">查询结果</div>
                            <div class="result">
                                <div class="result-item">店铺ID：{{ ruleForm.social_shop_no }}</div>
                                <div class="result-item">店铺名称：{{ ruleForm.title }}</div>
                            </div>
                        </div>
                    </el-form-item>
                </el-form>
            </div>
            <div
                class="dialog-footer"
                style="padding-top: 12px"
            >
                <el-button
                    size="medium"
                    style="width: 120px"
                    @click="dialogVisible = false"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="medium"
                    style="width: 120px;margin-left: 32px"
                    :disabled="ruleForm.social_shop_no === ''"
                    @click="handleConfirm()"
                >
                    确定
                </el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>

import {deepClone} from "@/assets/js/utils";
import Vue from "vue";

const Dialog = {
    name: "BindingShop",
    props: {
        shopNo: {
            type: String,
            default: ''
        },
        shopTitle: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            dialogVisible: false,
            // 搜索内容
            content: '',
            ruleForm: {
                shop_no: '',
                title: '',
                social_shop_no: '',
                // 1淘宝 2京东 3拼多多
                shop_social_type: 1
            }
        };
    },
    mounted() {},
    methods: {

        /**
         * 显示弹窗
         */
        show() {
            this.dialogVisible = true;
        },

        /**
         * 显示弹窗
         */
        close() {
            this.dialogVisible = false;
        },

        /**
         * 查询店铺
         */
        searchShop() {
            let params = {
                shop_social_type: this.ruleForm.shop_social_type,
                content: this.content
            };

            this.$post('/student/shop%5Csearch_social_shop', params, resp => {
                if (resp.code === 1) {
                    if(resp.data) {
                        this.ruleForm.title = resp.data.title;
                        this.ruleForm.social_shop_no = resp.data.social_shop_no;
                    } else {
                        this.$message.warning('未查询到相关店铺信息');
                    }
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 点击确定
         */
        handleConfirm() {
            let params = deepClone(this.ruleForm);

            params.shop_no = this.shopNo;
            params.shop_title = this.shopTitle;
            this.$post('/student/shop%5Cbind_social_shop', params, resp => {
                if (resp.code === 1) {
                    this.$notify({
                        title: '提示',
                        message: '绑定成功',
                        duration: 2000,
                        type: 'success'
                    });
                    this.$emit('confirm', true);
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },
    }
};

export default Dialog;

const bindingShopDialog = function (config) {
    return new Promise((resolve) => {
        const DialogConstructor = Vue.extend(Dialog);
        const instance = new DialogConstructor({
            propsData: config,
        }).$mount();

        instance.$on("close", function () {
            setTimeout(() => {
                document.body.removeChild(instance.$el);
                instance.$destroy();
            }, 300);
            resolve(false);
        });
        instance.$on("confirm", function (data) {
            instance.close();
            resolve(data);
        });
        document.body.appendChild(instance.$el);
        instance.dialogVisible = true;
    });
};

export { bindingShopDialog };
</script>

<style scoped lang="less">
.wrapper {
    .dialog-content {
        display: flex;
        flex-direction: column;
        padding: 0 32px;
        .query-button {
            width: 120px;
            height: 36px;
            background: #FFFFFF;
            border-radius: 4px;
            border: 1px solid #1890FF;
            font-size: 14px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #1890FF;
            margin-right: 12px;
            margin-left: 10px;
        }
        .result-view {
            width: 514px;
            height: 95px;
            background: #FBFBFB;
            border-radius: 4px;
            border: 1px solid #EBEEF5;
            padding: 0 20px;
            display: flex;
            flex-direction: column;
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #333333;
            line-height: 20px;
            .top {
                height: 44px;
                display: flex;
                align-items: center;
                border-bottom: 1px solid #EBEEF5;
            }
            .result {
                flex: 1;
                display: flex;
                align-items: center;
                &-item:first-of-type {
                    margin-right: 40px;
                }
            }
        }
    }
    .dialog-footer {
        display: flex;
        justify-content: center;
    }
}

/deep/ .el-dialog__header {
    padding-top: 10px;
    box-shadow: 0 1px 0 0 rgba(233, 233, 233, 1);
    display: flex;
    align-items: center;
    background: #FAFAFA;
    border-radius: 4px 4px 0 0;

    .el-dialog__title {
        font-weight: 500;
        color: #333;
        font-size: 14px;
    }

    .el-icon-close:before {
        color: rgb(194, 194, 194);
        font-size: 17px;
        font-weight: bold;
    }

    .el-dialog__headerbtn {
        top: 14px;
    }
}

/deep/ .el-dialog__body {
    padding: 24px 32px;
}
</style>
